home *** CD-ROM | disk | FTP | other *** search
- /*
- EventLog - TransDisplay event-logging demonstration program
-
- The project should include EventLog.c (this file), TransDisplay.c
- (or a project made from TransDisplay.c), TransSkel.c (or a project
- made from TransSkel.c), and MacTraps.
-
- 8 November 1986 Paul DuBois
- */
-
-
- # include <EventMgr.h>
- # include <MenuMgr.h>
- # include <ControlMgr.h>
- # include <FontMgr.h>
- # include "TransDisplay.h"
-
-
- enum /* declare zoom box part codes */
- {
- inZoomIn = 7,
- inZoomOut
- };
-
-
- # define maxButton 14
-
-
- # define helpTextRes 1000 /* help text resource number */
- # define aboutAlrtRes 1000 /* About... alert resource number */
-
-
- typedef enum /* Menu resource numbers */
- {
- fileMenuRes = 1000,
- editMenuRes,
- logMenuRes
- };
-
-
- typedef enum /* Window resource numbers */
- {
- logWindRes = 1000,
- helpWindRes,
- selectWindRes
- };
-
-
- typedef enum /* File menu item numbers */
- {
- showLog = 1, /* make windows visible/bring to front */
- showHelp,
- showSelect,
- /* --- */
- quit = 5
- };
-
-
- typedef enum /* Edit menu item numbers */
- {
- undo = 1,
- /* --- */
- cut = 3,
- copy,
- paste,
- clear
- };
-
-
- typedef enum /* Log menu item numbers */
- {
- logEvents = 1, /* whether events are logged */
- excludeLWind,
- /* --- */
- flushLog = 4, /* flush log output */
- /* --- */
- wrapStyle = 6, /* word wrap or not */
- /* --- */
- leftJust = 8, /* justification */
- centerJust,
- rightJust,
- /* --- */
- small = 12, /* text size */
- medium,
- large,
- /* --- */
- top = 16, /* scroll home */
- bottom /* scroll to bottom */
- };
-
-
- typedef struct CtrlInfo
- {
- Point loc; /* upper left of control */
- Str255 title; /* control title */
- Boolean *flagAddr; /* associated boolean */
- ControlHandle ctrl; /* associated control */
- struct CtrlInfo *subInfo; /* subsidiary control */
-
- } CtrlInfo;
-
-
- WindowPtr selectWind; /* event selection window */
- WindowPtr helpWind; /* help text window */
- WindowPtr logWind; /* log output window */
- MenuHandle fileMenu;
- MenuHandle editMenu;
- MenuHandle logMenu;
- Boolean reportEvents; /* report events or not */
- Boolean excludeLog; /* exclude log window events or not */
- int logFont;
- int logSize;
- int logWrap;
- int logJust;
-
-
- Boolean /* event type selection flags */
- rMouseDown = true,
- rMouseMods = false,
- rMouseWind = true,
- rMouseLoc = false,
- rMousePart = true,
- rMouseSys = false,
- rMouseUp = false,
- rKeyDown = true,
- rKDMods = false,
- rAutoKey = true,
- rAKMods = false,
- rUpdate = true,
- rActivate = true,
- rDisk = true;
-
-
- /*
- Control information. The last field is used to tell which controls
- are "owned" by another. When the owner is unchecked, all the owned
- controls go dim.
- */
-
- CtrlInfo ctrlInfo [maxButton] =
- {
- { { 5, 10}, "\pMouse Down", &rMouseDown, nil, nil },
- { { 25, 30}, "\pModifiers", &rMouseMods, nil, &ctrlInfo[0] },
- { { 45, 30}, "\pWindow", &rMouseWind, nil, &ctrlInfo[0] },
- { { 65, 30}, "\pLocation", &rMouseLoc, nil, &ctrlInfo[0] },
- { { 85, 30}, "\pPart Code", &rMousePart, nil, &ctrlInfo[0] },
- { { 105, 30}, "\pSystem Clicks", &rMouseSys, nil, &ctrlInfo[0] },
- { { 125, 10}, "\pMouse Up", &rMouseUp, nil, nil },
- { { 5, 160}, "\pKey Down", &rKeyDown, nil, nil },
- { { 25, 180}, "\pModifiers", &rKDMods, nil, &ctrlInfo[7] },
- { { 45, 160}, "\pAutoKey", &rAutoKey, nil, nil },
- { { 65, 180}, "\pModifiers", &rAKMods, nil, &ctrlInfo[9] },
- { { 85, 160}, "\pUpdate", &rUpdate, nil, nil },
- { { 105, 160}, "\pActivate", &rActivate, nil, nil },
- { { 125, 160}, "\pDisk", &rDisk, nil, nil }
- };
-
- /* Window that was in front last time checked */
-
- WindowPtr lastFront = nil;
-
-
- /*
- Print information about a window. If it's a window with a title,
- print the title. Print whether it's a
- desk accessory window.
- */
-
- WindowInfo (theWind)
- WindowPeek theWind;
- {
- Str255 title;
-
- GetWTitle (theWind, title);
- if (title[0] != 0) /* window has title */
- {
- DisplayChar (' ');
- DisplayString (title);
- }
-
- if (theWind->windowKind < 0)
- DisplayString ("\p (DA)");
- }
-
-
- Modifiers (mods)
- int mods;
- {
- DisplayString ("\p mods (0x");
- DisplayHexInt (mods);
- DisplayChar (')');
- }
-
-
- MouseLoc (thePt, thePort)
- Point thePt;
- GrafPtr thePort;
- {
- GrafPtr savePort;
-
- GetPort (&savePort);
- SetPort (thePort);
- GlobalToLocal (&thePt);
- SetPort (savePort);
- if (rMouseLoc)
- {
- DisplayString ("\p loc (");
- DisplayInt (thePt.h);
- DisplayString ("\p, ");
- DisplayInt (thePt.v);
- DisplayChar (')');
- }
- }
-
-
- /*
- Mouse click. Get the window that the click occurred in, and the
- part of the window.
-
- Make sure to get all the part codes! (incl. zoom box stuff)
- */
-
- ReportMouse (theEvent)
- EventRecord *theEvent;
- {
- Point evtPt;
- int evtPart;
- GrafPtr evtPort;
-
- evtPt = theEvent->where;
- evtPart = FindWindow (evtPt, &evtPort);
- if (excludeLog && evtPort == logWind)
- return;
- DisplayString ("\pMouse click");
-
- switch (evtPart)
- {
-
- /*
- Click in a desk accessory window.
- */
- case inSysWindow:
- if (rMouseSys)
- {
- if (rMousePart)
- DisplayString ("\p in system window:");
- if (rMouseWind)
- WindowInfo (evtPort);
- MouseLoc (evtPt, evtPort);
- }
- break;
-
- /*
- Click in desk top.
- */
- case inDesk:
- if (rMousePart)
- DisplayString ("\p in desktop");
- break;
-
- /*
- Click in menu bar.
- */
- case inMenuBar:
- if (rMousePart)
- DisplayString ("\p in menu bar");
- break;
-
- /*
- Click in grow box.
- */
- case inGrow:
- if (rMousePart)
- DisplayString ("\p in grow region:");
- if (rMouseWind)
- WindowInfo (evtPort);
- MouseLoc (evtPt, evtPort);
- break;
-
- /*
- Click in title bar.
- */
- case inDrag:
- if (rMousePart)
- DisplayString ("\p in drag region:");
- if (rMouseWind)
- WindowInfo (evtPort);
- break;
-
- /*
- Click in close box.
- */
- case inGoAway:
- if (rMousePart)
- DisplayString ("\p in close box:");
- if (rMouseWind)
- WindowInfo (evtPort);
- break;
-
- /*
- Click in zoom-in box.
- */
- case inZoomIn:
- if (rMousePart)
- DisplayString ("\p in zoom-in box:");
- if (rMouseWind)
- WindowInfo (evtPort);
- break;
-
- /*
- Click in zoom-out box.
- */
- case inZoomOut:
- if (rMousePart)
- DisplayString ("\p in zoom-out box:");
- if (rMouseWind)
- WindowInfo (evtPort);
- break;
-
- /*
- Click in content region.
-
- (Might also check in in control, and if so, print control information)
- */
- case inContent:
- if (rMousePart)
- DisplayString ("\p in content region:");
- if (rMouseWind)
- WindowInfo (evtPort);
- MouseLoc (evtPt, evtPort);
- break;
-
- }
- if (rMouseMods)
- Modifiers (theEvent->modifiers);
- DisplayLn ();
- }
-
-
- ReportKey (what, c, mods, modFlag)
- int what;
- char c;
- int mods;
- Boolean modFlag;
- {
- if (what == keyDown)
- DisplayString ("\pKey down: char '");
- else
- DisplayString ("\pAutokey: char '");
- DisplayChar (c);
- DisplayString ("\p' ");
- if (modFlag)
- Modifiers (mods);
- DisplayLn ();
- }
-
-
- ReportActivate (theWind, mods)
- WindowPtr theWind;
- int mods;
- {
- if ((mods & activeFlag) != 0)
- DisplayString ("\pActivate:");
- else
- DisplayString ("\pDeactivate:");
- WindowInfo (theWind);
- DisplayLn ();
- }
-
-
- ReportUpdate (theWind)
- WindowPtr theWind;
- {
- DisplayString ("\pUpdate:");
- WindowInfo (theWind);
- DisplayLn ();
- }
-
-
- /*
- General event logger
- */
-
- LogEvent (theEvt)
- EventRecord *theEvt;
-
- {
- register EventRecord *theEvent;
- Point evtPt;
- GrafPtr evtPort;
- register int evtPart;
- register char evtChar;
- register int evtMods;
- Rect r;
-
- if (reportEvents == false)
- return (false); /* don't do anything */
- theEvent = theEvt;
- evtPt = theEvent->where;
- switch (theEvent->what)
- {
-
- /*
- Mouse click.
- */
- case mouseDown:
- if (rMouseDown)
- ReportMouse (theEvent);
- break;
-
- case mouseUp:
- if (rMouseUp)
- DisplayString ("\pMouse up\r");
- break;
-
- /*
- Key event.
- */
- case keyDown:
- if (excludeLog && FrontWindow () == logWind)
- break;
- if (rKeyDown)
- {
- evtChar = theEvent->message & charCodeMask;
- evtMods = theEvent->modifiers;
- ReportKey (keyDown, evtChar, evtMods, rKDMods);
- }
- break;
-
- case autoKey:
- if (excludeLog && FrontWindow () == logWind)
- break;
- if (rKeyDown)
- {
- evtChar = theEvent->message & charCodeMask;
- evtMods = theEvent->modifiers;
- ReportKey (autoKey, evtChar, evtMods, rAKMods);
- }
- break;
-
- /*
- Update a window. If it's an update for the log window, invalidate
- it, because the message is written and will cause a scroll BEFORE
- the window actually gets updated. This means that part of what
- needs redrawing will be scrolled out of the update region and won't
- be redrawn properly. Invalidating the entire port is wasteful but
- makes sure the whole window can be drawn properly.
- */
- case updateEvt:
- if ((WindowPtr) theEvent->message == logWind)
- {
- SetPort (logWind);
- InvalRect (&logWind->portRect);
- }
- if (excludeLog && (WindowPtr) theEvent->message == logWind)
- break;
- if (rUpdate)
- ReportUpdate (theEvent->message);
- break;
-
- /*
- Activate or deactivate a window.
- */
- case activateEvt:
- if (excludeLog && (WindowPtr) theEvent->message == logWind)
- break;
- if (rActivate)
- ReportActivate (theEvent->message, theEvent->modifiers);
- break;
-
- /*
- handle inserts of uninitialized disks
- */
- case diskEvt:
- if (rDisk)
- {
- DisplayString ("\pDisk insertion");
- if (HiWord (theEvent->message) != noErr)
- {
- DisplayString ("\p (needs initializing)");
- }
- DisplayLn ();
- }
- break;
-
- }
- return (false);
- }
-
-
- /*
- Background procedure. Check front window, reset edit menu if window
- changes from an application window to a non-application window.
- Disable the Edit menu whenever an application window is active,
- enable it otherwise.
- Also called whenever it is known that the active window has changed.
- */
-
- CheckFront ()
- {
- WindowPtr curWind;
- int theKind;
- Boolean lastIsApp = false,
- curIsApp = false;
-
- curWind = FrontWindow ();
- if (IsDWindow (lastFront) || lastFront == selectWind)
- lastIsApp = true;
- if (IsDWindow (curWind) || curWind == selectWind)
- curIsApp = true;
- if (lastFront != curWind)
- {
- if (IsDWindow (lastFront) || lastFront == selectWind)
- lastIsApp = true;
- if (IsDWindow (curWind) || curWind == selectWind)
- curIsApp = true;
- if (lastIsApp != curIsApp)
- {
- theKind = 0;
- if (curWind != nil)
- theKind = ((WindowPeek) curWind)->windowKind;
- if (curWind == nil || theKind < 0) /* no window or DA in front */
- EnableItem (editMenu, 0);
- else
- DisableItem (editMenu, 0);
- DrawMenuBar ();
- }
- lastFront = curWind;
- }
- }
-
-
- /* ------------------------------------------------------------ */
- /* Event Selection Window Handler Routines */
- /* ------------------------------------------------------------ */
-
-
- /*
- Activate event procedure for both display windows and the checkbox
- window.
- */
-
- Activate (active)
- Boolean active;
- {
- CheckFront ();
- }
-
- /*
- Update window. This is easy, just draw the controls.
- */
-
- Update (resized)
- Boolean resized; /* ignored */
- {
- DrawControls (selectWind);
- }
-
-
- /*
- Handle hits in check boxes:
- Toggle check box, sync the associated flag, and enable or disable
- any subsidiary check boxes accordingly. (Subsidiaries have
- information in the control structure that points back to the owner
- check box.)
-
- */
-
- Mouse (thePt, t, mods)
- Point thePt;
- long t;
- int mods;
- {
- ControlHandle ctl;
- CtrlInfo *ci;
- Boolean val;
- int i;
-
- if (FindControl (thePt, selectWind, &ctl))
- {
- if (TrackControl (ctl, thePt, nil))
- {
- ci = (CtrlInfo *) GetCRefCon (ctl);
- val = !GetCtlValue (ctl);
- *(ci->flagAddr) = val;
- SetCtlValue (ctl, val);
-
- /* enable/disable any subsidiaries */
-
- for (i = 0; i < maxButton; ++i)
- {
- if (ctrlInfo[i].subInfo->ctrl == ci->ctrl)
- HiliteControl (ctrlInfo[i].ctrl, val ? 0 : 255);
- }
- }
- }
- }
-
-
- /*
- File menu handler
- */
-
- DoFileMenu (item)
- int item;
- {
- switch (item)
- {
- case showHelp:
- SelectWindow (helpWind);
- ShowWindow (helpWind);
- break;
-
- case showSelect:
- SelectWindow (selectWind);
- ShowWindow (selectWind);
- break;
-
- case showLog:
- SelectWindow (logWind);
- ShowWindow (logWind);
- break;
-
- case quit:
- SkelWhoa ();
- break;
-
- }
- }
-
- /*
- Put the right check marks in the Log menu
- */
-
- SetLogMenu ()
- {
- CheckItem (logMenu, logEvents, reportEvents);
- CheckItem (logMenu, excludeLWind, excludeLog);
- CheckItem (logMenu, wrapStyle, logWrap >= 0);
- CheckItem (logMenu, leftJust, logJust == teJustLeft);
- CheckItem (logMenu, centerJust, logJust == teJustCenter);
- CheckItem (logMenu, rightJust, logJust == teJustRight);
- CheckItem (logMenu, small, logSize == 9);
- CheckItem (logMenu, medium, logSize == 12);
- CheckItem (logMenu, large, logSize == 24);
- }
-
-
- /*
- Set display style of log window
- */
-
- SetStyle ()
- {
- SetDWindowStyle (logWind, logFont, logSize, logWrap, logJust);
- SetLogMenu ();
- }
-
-
- /*
- Log menu handler
- */
-
- DoLogMenu (item)
- int item;
- {
- switch (item)
- {
- case logEvents:
- reportEvents = !reportEvents;
- SetLogMenu ();
- break;
-
- case excludeLWind:
- excludeLog = !excludeLog;
- SetLogMenu ();
- break;
-
- case flushLog:
- FlushDWindow (logWind, 32767L);
- break;
-
- case wrapStyle:
- logWrap = (logWrap >= 0 ? -1 : 0);
- SetStyle ();
- break;
-
- case leftJust:
- logJust = teJustLeft;
- SetStyle ();
- break;
-
- case centerJust:
- logJust = teJustCenter;
- SetStyle ();
- break;
-
- case rightJust:
- logJust = teJustRight;
- SetStyle ();
- break;
-
- case small:
- logFont = monaco;
- logSize = 9;
- SetStyle ();
- break;
-
- case medium:
- logFont = systemFont;
- logSize = 12;
- SetStyle ();
- break;
-
- case large:
- logFont = geneva;
- logSize = 24;
- SetStyle ();
- break;
-
- case top:
- SetDWindowPos (logWind, 0);
- break;
-
- case bottom:
- SetDWindowPos (logWind, 32767);
- break;
- }
- }
-
-
- /*
- Handle selection of About… item from Apple menu
- */
-
- DoAbout ()
- {
- (void) Alert (aboutAlrtRes, nil);
- }
-
-
- /*
- Dispose of event selection window (and controls)
- */
-
- WClobber ()
- {
- DisposeWindow (selectWind);
- }
-
-
-
- /*
- Create controls
- */
-
- MakeControls (theWind)
- WindowPtr theWind;
- {
- int i;
- CtrlInfo *ci;
- Rect r;
-
- for (i = 0; i < maxButton; ++i)
- {
- ci = &ctrlInfo[i];
- SetRect (&r, ci->loc.h, ci->loc.v,
- ci->loc.h + StringWidth (ci->title) + 30,
- ci->loc.v + 20);
- ci->ctrl = NewControl (theWind, &r, ci->title, true,
- *(ci->flagAddr), 0, 1,
- checkBoxProc, ci);
- }
- ValidRect (&theWind->portRect);
- }
-
-
- main ()
- {
- Handle h;
-
- SkelInit ();
- SkelApple ("\pAbout EventLog…", DoAbout);
-
- fileMenu = GetMenu (fileMenuRes);
- SkelMenu (fileMenu, DoFileMenu, nil);
-
- editMenu = GetMenu (editMenuRes);
- DisableItem (editMenu, 0);
- SkelMenu (editMenu, nil, nil);
-
- logMenu = GetMenu (logMenuRes);
- SkelMenu (logMenu, DoLogMenu, nil);
-
- /*
- Create windows and install handlers.
- */
-
- SetDWindowNotify (nil, Activate);
-
- helpWind = GetNewDWindow (helpWindRes, -1L);
- SetDWindowStyle (helpWind, 0, 0, 0, teJustLeft);
-
- h = GetResource ('TEXT', helpTextRes); /* read help text */
- HLock (h); /* lock it and write to window */
- DisplayText (*h, GetHandleSize (h));
- HUnlock (h);
- ReleaseResource (h); /* done with it, so goodbye */
- SetDWindowPos (helpWind, 0); /* scroll back to top */
- ShowWindow (helpWind);
-
- logWind = GetNewDWindow (logWindRes, -1L);
-
- SkelEventHook (LogEvent);
- reportEvents = true;
- excludeLog = false;
-
- logFont = monaco;
- logSize = 9;
- logWrap = 0;
- logJust = teJustLeft;
- SetStyle ();
- ShowWindow (logWind);
-
- selectWind = GetNewWindow (selectWindRes, nil, -1L);
-
- SkelWindow (selectWind, /* the window */
- Mouse, /* mouse click handler */
- nil, /* key clicks are ignored */
- Update, /* window updating procedure */
- Activate, /* window activate/deactivate procedure */
- nil, /* hide window */
- WClobber, /* window disposal procedure */
- nil, /* idle proc */
- true); /* irrelevant */
-
- MakeControls (selectWind);
-
- /*
- Process events until user quits,
- then clean up and exit
- */
-
- CheckFront ();
- SkelBackground (CheckFront);
- SkelMain ();
- SkelClobber ();
- }
-